<template>
  <div class="shopcarContainer">
    <!-- 商品列表项区域 -->
    <div class="shopcar-goondslist">
      <div class="mui-card" v-for="(item,i) in goodslist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch v-model="$store.getters.getGoodsSelected[item.id]" @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
            <img :src="item.thumb_path" />
            <div class="info">
              <h1>{{ item.title }}</h1>
              <p>
                <span class="price">￥{{item.sell_price}}</span>
                <numbox :initCount="$store.getters.getGoodsInfoCount[item.id]" :goodsid="item.id"></numbox>
                <a href="#" @click.prevent="deleteGoodsInfo(item.id,i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 商品结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner jiesuan">
          <div class="left">
            <p>总价（不含运费）</p>
            <p>
              已勾选商品
              <span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span>件，总价
              <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
    <p>{{$store.getters.getGoodsSelected}}</p>
  </div>
</template>

<script>
import numbox from "../subcomponents/shopcar_numberbox";
export default {
  data() {
    return {
      goodslist: []
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      //获取store中所有商品的id，再拼接成字符串
      var idArr = [];
      this.$store.state.car.forEach(item => {
        idArr.push(item.id);
      });
      //如果购物车中没有商品，则直接返回，不需要请求数据接口，否则会报错
      if (idArr.length <= 0) {
        return;
      }
      this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then(res => {
          if (res.body.status === 0) {
            this.goodslist = res.body.message;
          }
        });
    },
    deleteGoodsInfo(id, index) {
      //1、删除store中数据根据id来删除；2、删除页面展示数据goodslist中的数据通过index
      this.goodslist.splice(index, 1);
      this.$store.commit("removeFromCar", id);
    },
    selectedChanged(id,val){
        console.log(id + ":" + val)
        this.$store.commit("updateSelected",{id:id,seleted:val})
    }
  },
  components: {
    numbox
  }
};
</script>

<style lang="scss" scoped>
.shopcarContainer {
  background-color: #eee;
  overflow: hidden;
  .shopcar-goondslist {
    .mui-card-content-inner {
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
    img {
      width: 60px;
      height: 60px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      h1 {
        white-space: nowrap;
        overflow: hidden;
        width: 180px;
        font-size: 13px;
        text-overflow: ellipsis;
      }
      .price {
        color: red;
        font-weight: bold;
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .red{
        color: red;
        font-size: 16px;
        font-weight: bold;
    }
  }
}
</style>